package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.DisinfectionResultDelayReceiveItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;

public class DisinfectionListThread extends BasicThread {

	public interface DisinfectionListThreadCallback {
		public void OnDisinfectionListCompleted(boolean isNetworkConntected, boolean results, String message);
		public void OnDisinfectionListError(ErrorItem errorItem);
	}

	private String url;
	private DisinfectionListThreadCallback callback;
	private HashMap<String, String> searchMap;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private boolean isFirst, pageState;
	private String offCd;

	public DisinfectionListThread(Context context, DisinfectionListThreadCallback callback, String offCd, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.offCd = offCd;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.isFirst = true;
	}
	
	public DisinfectionListThread(Context context,
			DisinfectionListThreadCallback callback, HashMap<String, String> searchMap, boolean pageState, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.searchMap = searchMap;
		this.isFirst = false;
		this.pageState = pageState;
	}

	@Override
	public void run() {
		
		Map<String, String> map = new HashMap<String, String>();
		String sDate = BrUtilManager.getCurrentDateTime(0);
		String eDate = BrUtilManager.getCurrentDateTime(0);
		String param = "";
		if(isFirst){ // 최초 요청
			map.put(Const.SEARCHOFFCD, offCd);
			map.put(Const.SEARCHSDATE, sDate);
			map.put(Const.SEARCHEDATE, eDate);
			param = parameterReturn(Const.REQ014, (HashMap<String, String>) map);
		}else{ // 조건 요청
			map=searchMap;
			param = parameterReturn(Const.REQ014, (HashMap<String, String>) map);
		}
		
		serviceBroker(param, context);
		
	}

	@Override
	public void settingObject() {
		
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnDisinfectionListCompleted(false, false, "통신 에러");
			return;
		}
	
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnDisinfectionListError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			ArrayList<DisinfectionResultDelayReceiveItem> arrdisinfectionItem = null;
			if(pageState){
				arrdisinfectionItem = appStatus.getArrDisinfectionItem();
			}else{
				arrdisinfectionItem = new ArrayList<DisinfectionResultDelayReceiveItem>();
			}
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);
	
				appStatus.setArrDisinfectionItem(arrdisinfectionItem);
				callback.OnDisinfectionListError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				DisinfectionResultDelayReceiveItem disinfectionItem = null;
				for (int i = 0; i < arrayLogin.length(); i++) {
					JSONObject disinfection = arrayLogin.getJSONObject(i);
					
					disinfectionItem = new DisinfectionResultDelayReceiveItem();
					disinfectionItem.setItmCnt(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.ITMCNT));
					disinfectionItem.setSumPrdlst(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.SUMPRDLST));
					disinfectionItem.setSumQyUnit(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.SUMQYUNIT));
					disinfectionItem.setDsnfTyNm(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.DSNFTYNM));
					disinfectionItem.setOpnDe(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.OPNDE));
					disinfectionItem.setOpnHh(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.OPNHH));
					disinfectionItem.setOpnMi(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.OPNMI));
					disinfectionItem.setCprNm(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.CPRNM));
					disinfectionItem.setPlanSn(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.PLANSN));
					disinfectionItem.setEntrpsUnitySn(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.ENTRPSUNITYSN));
					disinfectionItem.setAgreeCnt(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.AGREECNT));
					disinfectionItem.setDsnfCnt(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.DSNFCNT));
					disinfectionItem.setPlanCnt(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.PLANCNT));
					disinfectionItem.setTotalRows(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.TOTALROWS));
					disinfectionItem.setSumInspctReqstNo(appStatus.utilm.getStringJSONObject(disinfection, DisinfectionResultDelayReceiveItem.SUMINSPCTREQSTNO));
					
					arrdisinfectionItem.add(disinfectionItem);
				}
				appStatus.setArrDisinfectionItem(arrdisinfectionItem);
				callback.OnDisinfectionListCompleted(true, true, mResult);
			}
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}
}
